﻿using System;
using System.Collections.Generic;
using System.Collections;

using System.Text;

namespace Ecreate.Popedom.Service.SpringImpl
{
    public class MenuServiceImpl:IMenuService
    {
        Dao.Hibernate.MenuDaoImpl  menuDao = new Ecreate.Popedom.Dao.Hibernate.MenuDaoImpl();
        Dao.Hibernate.GroupDaoImpl groupDao = new Ecreate.Popedom.Dao.Hibernate.GroupDaoImpl();
        public string getXmlString()
        {
            IList<tMenu> l = menuDao.getAll();
            IList<tMenu> l_top = menuDao.getParentMenuList();
            StringBuilder xml = new StringBuilder();
            xml.Append("<?xml version='1.0' encoding='gb2312'?>");
            xml.Append("<tree id='0'>");
            foreach (tMenu p in l_top)
            {
                beginItem(p, xml);
                getChildXml(p, xml, l);
                endItem(xml);
            }
            xml.Append("</tree>");
            return xml.ToString();
        }
        public string getViewXmlStringByGroupId(long GroupId)
        {
            tGroup group = groupDao.getByID(GroupId);
            IList<tGroupassign> AssignList = group.Assigns;
            IList<tMenu> l = menuDao.getAll();
            IList<tMenu> l_top = menuDao.getParentMenuList();
            StringBuilder xml = new StringBuilder();
            xml.Append("<?xml version='1.0' encoding='gb2312'?>");
            xml.Append("<tree id='0'>");
            foreach (tMenu p in l_top)
            {
                bool ret=beginViewItem(p, xml, AssignList);
                if (!ret) continue;
                getViewChildXml(p, xml, l, AssignList);
                endItem(xml);
            }
            xml.Append("</tree>");
            return xml.ToString();

        }



        public string getXmlStringByGroupId(long GroupId)
        {
            tGroup group = groupDao.getByID(GroupId);
            IList<tGroupassign> AssignList = group.Assigns;
            IList<tMenu> l = menuDao.getAll();
            IList<tMenu> l_top = menuDao.getParentMenuList();
            StringBuilder xml = new StringBuilder();
            xml.Append("<?xml version='1.0' encoding='gb2312'?>");
            xml.Append("<tree id='0'>");
            foreach (tMenu p in l_top)
            {
                beginItem(p, xml, AssignList);
                getChildXml(p, xml, l,AssignList);
                endItem(xml);
            }
            xml.Append("</tree>");
            return xml.ToString();

        }
        private void getChildXml(tMenu p, StringBuilder xml, IList<tMenu> data)
        {
            IList<tMenu> childList = menuDao.getMenusByParentID(p.Id);
            foreach (tMenu pitem in childList)
            {
                beginItem(pitem, xml);
                getChildXml(pitem, xml, data);
                endItem(xml);
            }
        }
        private void getChildXml(tMenu p, StringBuilder xml, IList<tMenu> data, IList<tGroupassign> AssignList)
        {
            IList<tMenu> childList = menuDao.getMenusByParentID(p.Id);
            foreach (tMenu pitem in childList)
            {
                beginItem(pitem, xml,AssignList);
                getChildXml(pitem, xml, data,AssignList);
                endItem(xml);
            }
        }





        private void getViewChildXml(tMenu p, StringBuilder xml, IList<tMenu> data, IList<tGroupassign> AssignList)
        {
            IList<tMenu> childList = menuDao.getMenusByParentID(p.Id);
            foreach (tMenu pitem in childList)
            {
                bool ret=beginViewItem(pitem, xml, AssignList);
                if (!ret) continue;
                getViewChildXml(pitem, xml, data, AssignList);
                endItem(xml);
            }
        }



        private void beginItem(tMenu p, StringBuilder xml)
        {
            string img = "";
            string state = "";
            if (p.Menus !=null&&p.Menus.Count>0)
            {
                state = " select='1' open='1' ";
            }
            else
            {

            }
            string url = "/popedom/transfer.aspx?url={0}";
            url = string.Format(url, p.Menuurl);


            xml.Append((new StringBuilder("<item id='")).Append(p.Id).Append("' text='").Append(p.Menucaption).Append("' ").Append(img).Append(state).Append(">"));
            xml.Append("<userdata name='url'>");
            xml.Append(url);
            xml.Append("</userdata>");
        }

        private bool beginViewItem(tMenu p, StringBuilder xml, IList<tGroupassign> AssignList)
        {
            string img = "";
            string state = "";
            if (p.Menus != null && p.Menus.Count > 0)
            {
                state = " select='1' open='1' ";
            }
            else
            {

            }
            bool includemenu = this.AssinListIncludeMenu(p, AssignList);
            if (!includemenu) return false;
            if (includemenu)
            {
                state += " checked='1' ";
            }
            xml.Append((new StringBuilder("<item id='")).Append(p.Id).Append("' text='").Append(p.Menucaption).Append("' ").Append(img).Append(state).Append(">"));
            if (includemenu)
            {
                string url = "/popedom/transfer.aspx?url={0}";
                url = string.Format(url, p.Menuurl);
                xml.Append("<userdata name='url'>");
                xml.Append(url);
                xml.Append("</userdata>");
            }
            return true;
        }


        private void beginItem(tMenu p, StringBuilder xml,IList<tGroupassign> AssignList)
        {
            string img = "";
            string state = "";
            if (p.Menus != null && p.Menus.Count > 0)
            {
                state = " select='1' open='1' ";
            }
            else
            {

            }
            bool includemenu = this.AssinListIncludeMenu(p, AssignList);

            if (p.Menucaption == "订单录入")
            {
                int i = 0;
                bool includemenu1 = this.AssinListIncludeMenu(p, AssignList);
            }

            if (includemenu)
            {
                state += " checked='1' ";
            }
            xml.Append((new StringBuilder("<item id='")).Append(p.Id).Append("' text='").Append(p.Menucaption).Append("' ").Append(img).Append(state).Append(">"));

            if (includemenu)
            {
                string url = "/popedom/transfer.aspx?url={0}";
                url = string.Format(url, p.Menuurl);
                xml.Append("<userdata name='url'>");
                xml.Append(url);
                xml.Append("</userdata>");
            }
            else
            {
                string url = "/popedom/transfer.aspx?url={0}";
                url = string.Format(url, "");
                xml.Append("<userdata name='url'>");
                xml.Append(url);
                xml.Append("</userdata>");

            }
        }
        private void endItem(StringBuilder xml)
        {
            xml.Append("</item>");
        }
        private bool AssinListIncludeMenu(tMenu p, IList<tGroupassign> AssignList)
        {
            foreach (tGroupassign Assign in AssignList)
            {
                if (Assign.Menu.Id == p.Id)
                {
                    return true;
                }
            }
            return false;
        }

        public tMenu getById(string Id)
        {
           return this.menuDao.getById(Id);
        }

        public void InsertMenu(tMenu Model)
        {
            this.menuDao.Insert(Model);
        }
        public void UpdateMenu(tMenu Model)
        {
            this.menuDao.Update(Model);
        }
        public void DeleteMenu(tMenu Model)
        {
            this.menuDao.Delete(Model);
        }

        public IList<tMenu> getAll()
        {
            return this.menuDao.getAll();
        }
        public IList<tMenu> getSearchAll()
        {
            IList<tMenu> menuList = this.getAll();
            tMenu topMenu = new tMenu();
            topMenu.Id = null;
            topMenu.Menucaption = "顶级菜单";
            menuList.Insert(0, topMenu);
            return menuList;
        }
    }
}
